<?php

class SiteController extends Controller
{
    public $layout='';
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		$model_news = Event::model()->findRecentPosts(4);

        $model_today = Event::model()->findAll('date=UTC_DATE()');
        $model_tomorrow = Event::model()->findAll('date=UTC_DATE()+1');
        $model_thisweek = Event::model()->findAll('YEARWEEK(date)=YEARWEEK(UTC_DATE())');
        $model_thismonth = Event::model()->findAll('MONTH(date)=MONTH(UTC_DATE()) AND YEAR(date)=YEAR(UTC_DATE())');
		$this->render('index',array('model'=>array('model_news'=>$model_news,
            'model_time'=>array('model_today'=>$model_today,'model_tomorrow'=>$model_tomorrow,'model_thisweek'=>$model_thisweek,'model_thismonth'=>$model_thismonth,))));
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
	    if($error=Yii::app()->errorHandler->error)
	    {
	    	if(Yii::app()->request->isAjaxRequest)
	    		echo $error['message'];
	    	else
	        	$this->render('error', $error);
	    }
	}

	/**
	 * Displays the contact page
	 */
	public function actionContact()
	{
		$model=new ContactForm;
		if(isset($_POST['ContactForm']))
		{
			$model->attributes=$_POST['ContactForm'];
			if($model->validate())
			{
				$headers="From: {$model->email}\r\nReply-To: {$model->email}";
				mail(Yii::app()->params['adminEmail'],$model->subject,$model->body,$headers);
				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
				$this->refresh();
			}
		}
		$this->render('contact',array('model'=>$model));
	}

	/**
	 * Displays the login page
	 */
	public function actionLoginRegister()
	{
		$model_login=new LoginForm;

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model_login);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$model_login->attributes=$_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model_login->validate() && $model_login->login())
			{
				$message = 'Hello World!';
				$message .= '<br /><strong>Where do you want to go today?</strong><br />';
				$message .= CHtml::Button('Home', array('submit' => array('home/admin')));
				$message .= '<br />';
				$message .= CHtml::Button('Far far away', array('submit' => array('far/away')));
				Dialog::message('Title', $message );
				$this->redirect(Yii::app()->user->returnUrl);
			}
				
		}
        
        $model_register=new Users;
        $model_register->setScenario('register');
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
		if(isset($_POST['Users']))
		{
			$model_register->attributes=$_POST['Users'];
			if($model_register->save())
            {
                $this->redirect(array('site/index'));
            }
			 
		}
        
		// display the login&register form
		$this->render('loginregister',array('model'=>array('model_login'=>$model_login,'model_register'=>$model_register)));
	}

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
    
}